package com.drought.rained.ui.fragment;

import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.business.AuthRouterLJ;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.rxbus.EventMsgLJ;
import com.drought.rained.rxbus.RxBusLJ;
import com.google.gson.Gson;
import com.drought.rained.BuildConfig;
import com.drought.rained.R;
import com.drought.rained.LJApplication;
import com.drought.rained.base.BaseFragment;
import com.drought.rained.bean.AuthDataLJ;
import com.drought.rained.bean.UserInformationDataLJ;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.mvpview.AuthMvpViewLJ;
import com.drought.rained.mvpview.MineMvpViewLJ;
import com.drought.rained.presenter.AuthPresenterLJ;
import com.drought.rained.presenter.MinePresenterLJ;
import com.drought.rained.response.AuthResponseLJ;
import com.drought.rained.response.PersonalInfoResponseLJ;
import com.drought.rained.response.UserInformationResponseLJ;
import com.drought.rained.rxbus.EventsLJ;
import com.drought.rained.utils.MaskUtilsLJ;
import com.drought.rained.utils.NoDoubleClickUtilsLJ;
import com.drought.rained.utils.StringUtilsLJ;
import com.drought.rained.utils.ToastUtilsLJ;
import com.drought.rained.view.NormalDialogLJ;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MineFragmentLJ extends BaseFragment implements AuthMvpViewLJ, MineMvpViewLJ {

    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_auth_identity)
    TextView tvAuthIdentity;
    @BindView(R.id.btn_login_out)
    Button btnLoginOut;

    private AuthPresenterLJ authPresenter;
    private MinePresenterLJ minePresenter;
    private NormalDialogLJ mNormalDialog;

    private int totalStatus; // 认证状态
    private String orderBackReasonCode; // 资料退回Code
    private int isInformationWrong = 0;

    @Override
    public void onFragmentCreated(View rootView) {
        ButterKnife.bind(this, rootView);
        if (BuildConfig.DEBUG) {
            btnLoginOut.setVisibility(View.VISIBLE);
        } else {
            btnLoginOut.setVisibility(View.GONE);
        }
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_of_mine_layout;
    }

    @Override
    protected void onUIReady() {
        authPresenter = new AuthPresenterLJ(this);
        minePresenter = new MinePresenterLJ(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserInfoLJ.isLogin()) {
            authPresenter.getAuthStatus();
            minePresenter.getPersonInfo();
        }
    }

    @OnClick({R.id.layout_mine_bill, R.id.layout_mine_bank_card, R.id.layout_mine_auth, R.id.layout_mine_help,
            R.id.layout_mine_grant, R.id.btn_login_out})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_mine_bill:
                if (NoDoubleClickUtilsLJ.isDoubleClick()) return;
                ARouter.getInstance().build(RouterMapLJ.MINE_BILL).navigation();
                break;
            case R.id.layout_mine_bank_card:
                if (NoDoubleClickUtilsLJ.isDoubleClick()) return;
                if (totalStatus == 0) {
                    ARouter.getInstance().build(RouterMapLJ.FACEBOOKLOGIN).navigation();
                } else {
                    ARouter.getInstance().build(RouterMapLJ.MINE_BANK_CARD).navigation();
//                    if (totalStatus == 2) {
//                        ARouter.getInstance().build(RouterMapLJ.MINE_BANK_CARD).navigation();
//                    } else {
//                        ToastUtilsLJ.showShort(getActivity(), getResources().getString(R.string.mine_auth_verification_hint));
//                        EventBus.getDefault().post(totalStatus);
//                    }
                }
                break;
            case R.id.layout_mine_auth:
                if (NoDoubleClickUtilsLJ.isDoubleClick()) return;
                if (totalStatus == 0) {
                    ARouter.getInstance().build(RouterMapLJ.FACEBOOKLOGIN).navigation();
                } else if (totalStatus == 1) {
//                    AuthRouterLJ.getInstance().jump2FirstNotAuthPager();
                } else if (totalStatus == 2 && isInformationWrong == 1) {
                    AuthRouterLJ.getInstance().backRouter(orderBackReasonCode);
                }
                break;
            case R.id.layout_mine_help:
                ARouter.getInstance().build(RouterMapLJ.HELPCENTER_PAGE).navigation();
                break;
            case R.id.layout_mine_grant:
                ARouter.getInstance().build(RouterMapLJ.GRANTPERMISSION_PAGE).navigation();
                break;
            case R.id.btn_login_out:
                showLogoutDialog();
                break;
        }
    }

    private void showLogoutDialog() {
        if (mNormalDialog == null) {
            mNormalDialog = new NormalDialogLJ(getActivity(), R.style.normal_dialog)
                    .setTitle(getResources().getString(R.string.login_out_dialog_hint))
                    .setContent(getResources().getString(R.string.login_out_dialog_context))
                    .setNormalClickListener(new NormalDialogLJ.NormalClickListener() {
                        @Override
                        public void onLeftClick() {
                            mNormalDialog.dismiss();
                            tvUserName.setText("");
                            tvAuthIdentity.setText("");
                            UserInfoLJ.logout();
                            EventMsgLJ msg = new EventMsgLJ();
                            msg.setMsg(EventsLJ.LOGOUT);
                            RxBusLJ.getInstance().post(msg);
                        }

                        @Override
                        public void onRightClick() {
                            mNormalDialog.dismiss();
                        }
                    });
        }
        WindowManager.LayoutParams params = mNormalDialog.getWindow().getAttributes();//获取dialog信息
        params.width = 660;
        mNormalDialog.getWindow().setAttributes(params);//设置大小

        mNormalDialog.show();
    }

    @Override
    public void onGetDataNext(AuthResponseLJ response) {
        String jsonString = new Gson().toJson(response.getData(), AuthDataLJ.class);
        AcacheLJ.get(LJApplication.getContext()).put(AcacheKeysLJ.AUTH, jsonString);
        totalStatus = response.getData().getStatus();
        if (totalStatus == 2) {
            minePresenter.getUserInformation();
        }
    }

    @Override
    public void onGetDataNext(PersonalInfoResponseLJ response) {
        if (response.getStatus() == ConstantLJ.HTTP_OK) {
            MaskUtilsLJ.setText(tvUserName, response.getData().getUserName());
        } else {
            ToastUtilsLJ.showShort(getActivity(), response.getMessage());
        }
    }

    @Override
    public void onGetDataNext(UserInformationResponseLJ response) {
        final UserInformationDataLJ data = response.getData();
        isInformationWrong = data.isInformationWrong();
        if (data.isInformationWrong() == ConstantLJ.USER_DATA_BACK) {
            if (tvAuthIdentity != null) {
                tvAuthIdentity.setText(data.getOrderBackReason());
                if (!StringUtilsLJ.isEmpty(data.getAuthPageCode())) {
                    orderBackReasonCode = data.getAuthPageCode();
                    AcacheLJ.get(LJApplication.getContext()).put(AcacheKeysLJ.BACK, data.getUserUuid());
                }
            }
        } else {
            AcacheLJ.get(LJApplication.getContext()).put(AcacheKeysLJ.BACK, "");
            tvAuthIdentity.setText("");
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {}
}
